package com.hairline.aspect;

import com.hairline.bean.Footprint;
import com.hairline.bean.User;
import com.hairline.service.WxFootprintService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@Aspect
@Component
public class UserFootprintLoggerAspect {

    @Autowired
    WxFootprintService wxFootprintService;

    @Pointcut("@annotation(UserFootprintLog)")
    public void userFootprintLogPointcut() {}

    @Around("userFootprintLogPointcut()")
    public Object AroundFootprintLog(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入切面");
        Footprint footprint = new Footprint();
        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();

        //获取请求参数
        Object[] args = joinPoint.getArgs();
        Integer goodsId = (Integer) args[0];
        footprint.setGoodsId(goodsId);
        footprint.setUserId(userId);

        //记录至数据库
        wxFootprintService.log(footprint);
        //continue
        Object proceed = joinPoint.proceed();
        return proceed;
    }
}
